package yns.springboot.guide;

import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;


//控制台指定配置文件启动 : java -jar .\SpringBootTest-0.0.1-SNAPSHOT.jar --spring.profiles.active=test
//IDE指定配置文件启动  : 在Run或者Debug的配置arguments加入对应的 --spring.profiles.active=test


@SpringBootApplication
//@MapperScan("yns.springboot.guide.demo.data.db.mybatis.dao") 
@EnableCaching
@ConfigurationPropertiesScan
//启动类建议放在包的根路径下，比如包路径为com.test，那么启动类建议放在com下面，如果放在其他路径下需要使用ComponentScan添加指定的包路径，让对应的类由spring容器管理
//@ComponentScan("yns.springboot.guide")  
public class SpringBootSetup {

	private static final Logger logger = LoggerFactory.getLogger(SpringBootSetup.class);
	
	
	private static final String DEFAULT_MESSAGE = "Does Key exist or null ?";
	
	private static final String DEFAULT_URL = "localhost";
	
	private static final String SERVER_PORT = "server.port";
	
	
    public static void main(String[] args) throws Exception {
		ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(SpringBootSetup.class,
				args);
		Environment environment = configurableApplicationContext.getEnvironment();

		logEnvironment(environment);
    }
    
	private static void logEnvironment(Environment environment) {
		Object[] objects = new Object[5];
		String format = "Environment Configure : \n" 
				+ "default_url : {} \n"
				+ "server.port : {} \n" ;
		
		objects[0] = Optional.ofNullable(DEFAULT_URL).orElse(DEFAULT_MESSAGE);
		objects[1] = Optional.ofNullable(environment.getProperty(SERVER_PORT)).orElse(DEFAULT_MESSAGE);

		logger.info(format, objects);
	}
}
